package com.xin.springclouduser.controller;

import com.xin.springclouduser.dto.UserDTO;
import com.xin.springclouduser.enums.MyFriendEnum;
import com.xin.springclouduser.service.MyFriendService;
import com.xin.springclouduser.utils.ResultVOUtils;
import com.xin.springclouduser.vo.ResultPage;
import com.xin.springclouduser.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/friend")
public class MyFriendController {
    @Autowired
    private MyFriendService myFriendService;

    /**
     * 创建好友关系
     * @param uid
     * @param fid
     * @return
     */
    @PostMapping("/create")
    public ResultVO create(@RequestParam(name = "uid",defaultValue = "")String uid,
                           @RequestParam(name = "fid",defaultValue = "")String fid){
        boolean b = myFriendService.create(uid, fid);
        if (!b){
           return ResultVOUtils.error(1, MyFriendEnum.CREATE_FRIEND.getMessage());
        }
        return ResultVOUtils.success();
    }

    /**
     * 删除好友关系
     * @param uid
     * @param fid
     * @return
     */
    @GetMapping("/delete")
    public ResultVO delete(@RequestParam(name = "uid",defaultValue = "")String uid,
                           @RequestParam(name = "fid",defaultValue = "")String fid){
        boolean b = myFriendService.delete(uid, fid);
        if (!b){
            return ResultVOUtils.error(1,MyFriendEnum.DELETE_FRIEND.getMessage());
        }
        return ResultVOUtils.success();
    }

    /**
     * 是否为好友关系
     * @param uid
     * @param fid
     * @return
     */
    @GetMapping("/findFriendShip")
    public ResultVO findFriendShip(@RequestParam(name = "uid",defaultValue = "") String uid,
                                   @RequestParam(name = "fid",defaultValue = "") String fid){
        boolean b = myFriendService.findFriendShip(uid, fid);
        if (!b){
              return ResultVOUtils.error(1,MyFriendEnum.NOT_FRIEND.getMessage());
        }
        return ResultVOUtils.success();
    }

    /**
     * 查询好友列表
     * @param uid
     * @param pageable
     * @return
     */
    @GetMapping("/findMyFriendList")
    public ResultPage findMyFriendList(@RequestParam(name = "uid",defaultValue = "")String uid,
                                       @PageableDefault(page = 1,size = 10)Pageable pageable){
        PageRequest pageRequest=new PageRequest(pageable.getPageNumber()-1,pageable.getPageSize());
        ResultPage<UserDTO> resultPage = myFriendService.findMyFriendList(uid, pageRequest);
        return resultPage;
    }
}
